import pytest
import requests


class TestA3MallLogin:
    def test_A3mall_01_login(self):
        """测试商城登录接口正常流程"""
        url = 'http://10.59.9.14:7003/api/public/login'
        payload = {
            "username": "17733058407",
            "password": "li123456"
        }

        try:
            # 带超时和异常处理的请求
            response = requests.post(url, json=payload, timeout=3)

            # 验证HTTP状态码
            assert response.status_code == 200, f"异常HTTP状态码：{response.status_code}"

            # 解析响应数据
            response_data = response.json()
            print("\n响应数据:", response_data)

            # 验证业务逻辑状态码
            assert response_data.get('code') == 0, f"业务异常码：{response_data.get('code')}"

            # 验证关键字段存在性
            assert 'access_token' in response_data, "响应缺少access_token"
            assert isinstance(response_data['access_token'], str), "access_token格式异常"

        except requests.exceptions.RequestException as e:
            pytest.fail(f"网络请求异常: {str(e)}")
        except ValueError:
            pytest.fail("响应数据非JSON格式")

# 命令行执行方式（不需要main函数）：
# pytest -v test_login.py